package com.sgb.kjwl.view.ui.fragment.freightTransport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMapGoodsCarsBinding;
import com.swgk.core.event.EventConstant;
import com.swgk.core.event.MenuEvent;
import com.sgb.kjwl.view.ui.fragment.home.FindCarMapAndListFragment;
import com.sgb.kjwl.view.ui.fragment.transport.GoodsHallListFragment;
import com.sgb.kjwl.view.ui.fragment.transport.MapCarsFragment;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author: 王彬臣
 * @date:
 * @Description: 物流图查
 */
public class MapGoodsAndCarFragment extends AppLazyFragment {
    public static final int GOODS_INFO_FRAGMENT = 0;
    public static final int VEHICLE_INFO_FRAGMENT = 1;
    public static final int BIDDING_FREIGHT_FRAGMENT = 2;
    public int currentNeedShowIndex = GOODS_INFO_FRAGMENT;

    FragmentMapGoodsCarsBinding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    String[] titles = {"货运大厅", "货车大厅", "竞价货运", "司机找货"};

    public static MapGoodsAndCarFragment getInstance(boolean isShowBack) {
        MapGoodsAndCarFragment fragment = new MapGoodsAndCarFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isShowBack", isShowBack);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_map_goods_cars, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    protected void initView() {
        if (getArguments() != null && getArguments().getBoolean("isShowBack", false)){
            binding.ivBack.setVisibility(View.VISIBLE);
            binding.ivBack.setOnClickListener(this);
        }

        mFragments = new ArrayList<>();
        mFragments.add(GoodsHallListFragment.newInstance(0, true, true, false, true));
        mFragments.add(MapCarsFragment.newInstance(false));
        mFragments.add(GoodsHallListFragment.newInstance(1, true, true, false, true));
        mFragments.add(FindCarMapAndListFragment.newInstance(true, false));
        pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                currentNeedShowIndex = position;

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (binding.viewpager.getCurrentItem() != currentNeedShowIndex) {
            binding.viewpager.setCurrentItem(currentNeedShowIndex);
            currentNeedShowIndex = GOODS_INFO_FRAGMENT;
        }

    }

    public void setCurrentFragment(int fragmentIndex) {
        currentNeedShowIndex = fragmentIndex;
        switch (fragmentIndex) {
            case GOODS_INFO_FRAGMENT:
                binding.viewpager.setCurrentItem(GOODS_INFO_FRAGMENT);
                break;

            case VEHICLE_INFO_FRAGMENT:
                binding.viewpager.setCurrentItem(VEHICLE_INFO_FRAGMENT);
                break;

            case BIDDING_FREIGHT_FRAGMENT:
                binding.viewpager.setCurrentItem(BIDDING_FREIGHT_FRAGMENT);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(MenuEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PAGE_SKIP_TUCHA) {
            int tabPosition = (int) event.getMessageData();
                switch (tabPosition) {
                    case 0:
                        setCurrentFragment(GOODS_INFO_FRAGMENT);
                        break;
                    case 1:
                        setCurrentFragment(VEHICLE_INFO_FRAGMENT);
                        break;
                    case 2:
                        setCurrentFragment(BIDDING_FREIGHT_FRAGMENT);
                        break;

                }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
